
Partial Class Case_caseMedicalProvider
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Const C_EDITCOLUMN As Integer = 4
    Const C_VIEW_COLUMN As Integer = 5
    Const C_DELETE_COLUMN As Integer = 6

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return "Medical Provider"
        End Get
    End Property

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseMedicalProvider
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Dim pg As IPermission = Me.Page
            Return pg.PrimaryObject
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)

        End Set
    End Property

    Public Property ParticipantObject() As Cdm.DataCore.Entities.Participant
        Get
            Dim permissionPage As IPermission = Me.Page
            Return permissionPage.PrimaryObject
        End Get
        Set(ByVal value As Cdm.DataCore.Entities.Participant)
            BindPage()
            SetPageProperties()
        End Set
    End Property

    Private ReadOnly Property AddVisible() As Boolean
        Get
            Return UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End Get
    End Property

    Private Property EditIndex() As Integer
        Get
            If ViewState("editIndex") Is Nothing Then
                Return -1
            Else
                Return Convert.ToInt32(ViewState("editIndex"))
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("editIndex") = value
        End Set
    End Property

    Private Sub SetPageProperties()

        Dim newPhysician As New Cdm.DataCore.Entities.Physician()
        Dim setProperties As Boolean = Not Me.IsPostBack
        Dim isEditable As Boolean = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        EntityController.SetPagePropertiesFromObject(Me.DetailPanel.Controls, newPhysician, isEditable, setProperties, newPhysician.EntityTableName)
        Me.SpecialFieldControl.Editable = isEditable
        Me.SpecialFieldControl.PrimaryObject = newPhysician
        SetPermissionView()

    End Sub

    Private Sub SetPermissionView()

        Me.AddProviderButton.Visible = Me.AddVisible
        Me.MedicalGridView.Columns(C_DELETE_COLUMN).Visible = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        If UserAuthorization.HasUpdatePermission(Me.EntityPermissionType) Then
            Me.MedicalGridView.Columns(C_EDITCOLUMN).Visible = True
            Me.MedicalGridView.Columns(C_VIEW_COLUMN).Visible = False
            Me.SaveButton.Visible = True
            Me.CancelButton.Text = "Cancel"
        Else
            Me.MedicalGridView.Columns(C_EDITCOLUMN).Visible = False
            Me.MedicalGridView.Columns(C_VIEW_COLUMN).Visible = True
            Me.SaveButton.Visible = False
            Me.CancelButton.Text = "Return to Grid"
        End If

    End Sub

    Private Sub BindPage()

        Me.SpecialtyInputControl.DataSource = Cdm.DataCore.LookupTables.LuMedicalSpecialty.ListAll()
        Me.SpecialtyInputControl.DataTextField = "Description"
        Me.SpecialtyInputControl.DataValueField = "SpecialtyId"
        Me.SpecialtyInputControl.DataBind()
        BindGrid()

    End Sub

    Private Sub BindGrid()

        Me.MedicalGridView.DataSource = Me.ParticipantObject.Physicians
        Me.MedicalGridView.DataBind()

    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls
        Me.NameInputControl.Value = ""
        Me.Address1InputControl.Value = ""
        Me.Address2InputControl.Value = ""
        Me.FaxNumberInputControl.Value = ""
        Me.PhoneNumber1InputControl.Value = ""
        Me.SpecialtyInputControl.Value = ""
        Me.StateInputControl.Value = ""
        Me.ZipCodeInputControl.Value = ""
        Me.CityInputControl.Value = ""
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        Me.validateMedicalProvider.Visible = False
        If Me.IsPostBack Then
            SetPageProperties()
        End If

    End Sub

    Protected Sub AddProviderButton_Click(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                                          Handles AddProviderButton.Click

        ShowPanel()

    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles SaveButton.Click

        Dim editPhysician As Cdm.DataCore.Entities.Physician
        If Me.EditIndex = -1 Then
            editPhysician = New Cdm.DataCore.Entities.Physician
            Me.VerifyUpdateLabel.Text = "Medical Provider added.  Click 'Save File' to add this Provider to the file permanently."
        Else
            editPhysician = Me.ParticipantObject.Physicians(Me.EditIndex)
            Me.VerifyUpdateLabel.Text = "Medical Provider updated.  Click 'Save File' to save your changes to the file permanently."
        End If
        editPhysician.Reset()
        EntityController.SetObjectPropertiesFromPage(Me.Controls, editPhysician, Me.SectionName)
        editPhysician.SpecialFieldsValues = Me.SpecialFieldControl.SaveSpecialFields()

        Dim errorList As ArrayList = editPhysician.Validate()
        If errorList.Count > 0 Then
            Me.ValidateMedicalProvider.SetErrors(errorList)
            Me.ValidateMedicalProvider.Visible = True
            If editPhysician.PhysicianId > 0 Then
                editPhysician.Refresh()
            End If
        Else
            If Me.EditIndex = -1 Then
                Me.ParticipantObject.Physicians.Add(editPhysician)
            End If
            Me.EditIndex = -1
            Me.VerifyUpdateLabel.Visible = True
            ResetControls()
            ShowGrid()
        End If

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles CancelButton.Click

        ResetControls()
        ShowGrid()

    End Sub

    Private Sub ShowGrid()

        Me.DetailPanel.Visible = False
        Me.AddProviderButton.Visible = Me.AddVisible
        Me.MedicalGridView.Visible = True
        Dim savePage As IPageSave = Me.Page
        savePage.SaveVisible = True
        BindGrid()

    End Sub

    Private Sub ShowPanel()

        Me.AddProviderButton.Visible = False
        Me.MedicalGridView.Visible = False
        Me.DetailPanel.Visible = True
        Dim savePage As IPageSave = Me.Page
        savePage.SaveVisible = False

    End Sub

    Protected Sub MedicalGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles MedicalGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(MedicalGridView)
    End Sub


    Protected Sub MedicalGridView_RowDeleting(ByVal sender As Object, _
                                              ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) _
                                              Handles MedicalGridView.RowDeleting

        Me.ParticipantObject.DeletePhysician(e.RowIndex)
        e.Cancel = True
        BindGrid()

    End Sub

    Protected Sub MedicalGridView_RowEditing(ByVal sender As Object, _
                                             ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
                                             Handles MedicalGridView.RowEditing

        Me.EditIndex = e.NewEditIndex
        Dim isEditable As Boolean = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        EntityController.SetPageValuesFromObject(Me.DetailPanel.Controls, Me.ParticipantObject.Physicians(e.NewEditIndex), isEditable)
        Me.SpecialFieldControl.Editable = isEditable
        Me.SpecialFieldControl.IsBound = False
        Me.SpecialFieldControl.PrimaryObject = Me.ParticipantObject.Physicians(e.NewEditIndex)
        e.Cancel = True
        ShowPanel()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        Me.VerifyUpdateLabel.Visible = False

    End Sub

End Class
